// ！！只引入核心编辑器功能
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'

// 代码高亮
import 'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution'
import 'monaco-editor/esm/vs/basic-languages/typescript/typescript.contribution'
import 'monaco-editor/esm/vs/language/typescript/monaco.contribution'

// language worker
import TsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'

// monaco功能， 全量功能见node_modules\monaco-editor\esm\metadata.js
import 'monaco-editor/esm/vs/editor/contrib/comment/browser/comment'
import 'monaco-editor/esm/vs/editor/contrib/contextmenu/browser/contextmenu'
import 'monaco-editor/esm/vs/editor/contrib/cursorUndo/browser/cursorUndo'
import 'monaco-editor/esm/vs/editor/contrib/find/browser/findController'
import 'monaco-editor/esm/vs/editor/contrib/hover/browser/hover'
import 'monaco-editor/esm/vs/editor/contrib/suggest/browser/suggestController'
import 'monaco-editor/esm/vs/editor/contrib/suggest/browser/suggestInlineCompletions'
import 'monaco-editor/esm/vs/editor/contrib/format/browser/formatActions'

function initMonaco() {
  globalThis.MonacoEnvironment = {
    getWorker(_: any, label: string) {
      if (label === 'json')
        return new JsonWorker()

      if (label === 'typescript' || label === 'javascript')
        return new TsWorker()

      return new EditorWorker()
    },
  }
}

export { monaco, initMonaco }
